package com.ztgf.shopping.api.cms.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.shop.ShopShoppingCartCmsBo;
import com.ztgf.shopping.models.vo.shop.ShopShoppingCartCmsVO;
import com.ztgf.shopping.models.vo.shop.ShopShoppingCartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import com.ztgf.shopping.models.service.shop.IShopShoppingCartService;
import com.ztgf.shopping.models.bo.shop.ShopShoppingCartSaveBO;
import com.ztgf.shopping.models.bo.shop.ShopShoppingCartBO;
import com.ztgf.shopping.models.dto.shop.ShopShoppingCartDTO;

/**
 * 用户购物车表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-27 14:05:12
 */

@RestController
@RequestMapping("/cms/shopShoppingCart")
@Slf4j
@Api(value = "ShopShoppingCartController",tags = "用户购物车表接口")
public class ShopShoppingCartController {

    @DubboReference
    private IShopShoppingCartService shopShoppingCartService;

    @ApiOperation(value = "添加用户购物车表",notes = "添加用户购物车表")
    @PostMapping(value = "/insert")
    public Result insert( ShopShoppingCartSaveBO saveBO) {
        boolean flag = shopShoppingCartService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "admin添加用户购物车表",notes = "admin添加用户购物车表")
    @PostMapping(value = "/add")
    public Result add( String purchaseCommodityBargainIds) {
            return shopShoppingCartService.addByCms(purchaseCommodityBargainIds);

    }

    @PostMapping("/save")
    @ApiOperation(value = "保存用户购物车表",notes = "保存用户购物车表")
    public Result save( ShopShoppingCartSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改用户购物车表",notes = "修改用户购物车表")
    @PostMapping("/update")
    public Result update( ShopShoppingCartSaveBO saveBO){
        ShopShoppingCartDTO dto = new ShopShoppingCartDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = shopShoppingCartService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除用户购物车表",notes = "删除用户购物车表")
    @ApiImplicitParam(name = "id",value = "用户购物车表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = shopShoppingCartService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除用户购物车表",notes = "批量删除用户购物车表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        shopShoppingCartService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopShoppingCartDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopShoppingCartDTO dto = shopShoppingCartService.selectById(id);
        //debug 需要根据商品ID和商品规格查出商品信息的接口
        return new Result<>(dto);
    }

//    @ApiOperation(value = "用户购物车表列表",notes = "用户购物车表列表")
//    @PostMapping("/queryPageList")
//    public Result<Page<ShopShoppingCartDTO>> queryPageList( ShopShoppingCartBO bo){
//        Wrapper<ShopShoppingCartDTO> wrapper = new EntityWrapper<>();

        //debug 需要根据商品ID和商品规格查出商品信息的接口
//
//        page = shopShoppingCartService.selectPage(page, wrapper);
//
//        return new Result<>(page);
//    }


    @ApiOperation(value = "admin购物车表列表", notes = "admin购物车表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopShoppingCartCmsVO>> queryPageList(ShopShoppingCartCmsBo bo) {
        Page<ShopShoppingCartCmsVO> page = shopShoppingCartService.queryShoppingCartPageListByAdmin(bo);

        return new Result<>(page);
    }
}
